import 'package:flutter/material.dart';
import 'package:flutter_staggered_grid_view/flutter_staggered_grid_view.dart';
import 'package:flutter_syt/datas/game_item.dart';
import 'package:flutter_syt/datas/game_subcription_item.dart';
import 'package:flutter_syt/domain/widgets/subscription_manage_item.dart';
import 'package:flutter_syt/generated/l10n.dart';
import 'package:flutter_syt/theme/font_styles.dart';
import 'package:flutter_syt/theme/theme_color.dart';
import 'package:provider/provider.dart';

class MySubscriptionItem {
  late String code;
  late String name;
  late String type;
  MySubscriptionItem(this.code, this.name, this.type);
}

class SubscriptionManageMy extends StatefulWidget {
  final ScrollController scrollController;
  final List<GameDataItem> mySubscriptionList;
  const SubscriptionManageMy({
    required this.scrollController,
    this.mySubscriptionList = const [],
  });

  @override
  _SubscriptionManageMyState createState() => _SubscriptionManageMyState();
}

class _SubscriptionManageMyState extends State<SubscriptionManageMy> {
  // ScrollController _scrollController = ScrollController();

  Widget emptyContainer() {
    return Container(
      margin: EdgeInsets.only(bottom: 20),
      child: Text(
        S().Choose_keywords_that_interest_you,
        style: FontStyles.headerTextStyle_16.copyWith(
          // fontWeight: FontWeight.w500,
          color: ThemeColor.colorBlack40,
        ),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Consumer<GameSubcriptionManageData>(builder: (ctx, data, child) {
      List<MySubscriptionItem> mySubscriptionList = [];
      mySubscriptionList.addAll(data.discipline
          .map(
            (e) => MySubscriptionItem(
              e.discipline,
              e.disciplineName,
              'discipline',
            ),
          )
          .toList());
      mySubscriptionList.addAll(data.venue
          .map(
            (e) => MySubscriptionItem(
              e.code,
              e.venueName,
              'venue',
            ),
          )
          .toList());
      return Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        mainAxisSize: MainAxisSize.min,
        children: [
          Text(
            S().My_Subscriptions,
            style: FontStyles.headerTextStyle_16.copyWith(
              fontWeight: FontWeight.w500,
            ),
          ),
          SizedBox(
            height: 10,
          ),
          mySubscriptionList.length > 0
              ? Container(
                  // color: Colors.red,
                  child: StaggeredGridView.countBuilder(
                    shrinkWrap: true,
                    padding: EdgeInsets.only(bottom: 0),
                    controller: widget.scrollController,
                    crossAxisCount: 3,
                    crossAxisSpacing: 12,
                    mainAxisSpacing: 16,
                    itemCount: mySubscriptionList.length,
                    itemBuilder: (context, index) {
                      MySubscriptionItem subscription =
                          mySubscriptionList[index];
                      return SubscriptionManageItem(
                        subscription.name,
                        mode: SubscriptionManageItemMode.unSubscribe,
                        subscribed: true,
                        onUnSubscribe: (String entity) {
                          GameSubcriptionManageData.remove(
                            subscription.code,
                            subscription.type,
                          );
                        },
                      );
                    },
                    staggeredTileBuilder: (index) => StaggeredTile.fit(1),
                  ),
                )
              : emptyContainer(),
        ],
      );
    });
  }
}
